package com.jrx.anytxn.transaction.job.autoPayment.config;

import com.jrx.anytxn.param.entity.PrOrganizationTable;
import com.jrx.anytxn.param.service.system.IOrganizationTableService;
import com.jrx.anytxn.transaction.config.AnytxnBatchProperties;
import com.jrx.anytxn.transaction.constant.TransBizConstant;
import com.jrx.anytxn.transaction.entity.BtAutoPaymentList;
import com.jrx.anytxn.transaction.entity.TlPaymentLog;
import com.jrx.anytxn.transaction.job.autoPayment.step.*;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.item.ItemReader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.SimpleAsyncTaskExecutor;
import org.springframework.transaction.PlatformTransactionManager;

import javax.annotation.Resource;
import java.util.Date;

/**
 * 批扣第三次初始化
 *
 * @Author yhy
 * @Date 2023/4/25 9:00
 */
@Configuration
@EnableConfigurationProperties(AnytxnBatchProperties.class)
public class AutoPaymentThirdInitConfiguration {
    @Resource(name = "bizSqlSessionFactory")
    SqlSessionFactory sqlSessionFactory;

    @Autowired
    public JobBuilderFactory jobBuilderFactory;

    @Autowired
    public StepBuilderFactory stepBuilderFactory;

    @Autowired
    private AnytxnBatchProperties anytxnBatchProperties;

    @Autowired
    private AutoPaymentInitListener autoPaymentInitListener;

    @Autowired
    private PlatformTransactionManager platformTransactionManager;

    @Autowired
    private IOrganizationTableService organizationTableService;

    @Autowired
    private AutoPaymentThirdInitProcessor autoPaymentThirdInitProcessor;

    @Autowired
    private AutoPaymentThirdInitWriter autoPaymentThirdInitWriter;

    /**
     * 第三次批扣初始化
     * @return
     */
    @Bean
    public Job autoPaymentThirdInitJob(){
        return jobBuilderFactory.get("autoPaymentThirdInitJob")
                //清空批扣初始化列表
                .listener(autoPaymentInitListener)
                // 初始化上次批扣失败记录
                .start(autoPaymentThirdFailInitStep())
                // 初始化上次非足额扣款成功记录
                .next(autoPaymentThirdPartSuccessInitStep())
                .build();
    }

    @Bean
    public Step autoPaymentThirdFailInitStep(){
        return stepBuilderFactory.get("autoPaymentThirdFailInitStep")
                // 表示一批处理100条记录，数据是逐条发送给processor处理的，处理100条记录后，统一返回给writer进行保存处理
                .<TlPaymentLog, BtAutoPaymentList>chunk(anytxnBatchProperties.getAutoPaymentInitConfiguration().getChunkLimit())
                // 获取上次批扣失败流水
                .reader(autoPaymentThirdFailInitReader())
                // 处理组装批扣初始化数据
                .processor(autoPaymentThirdInitProcessor)
                // 保存批扣初始化数据
                .writer(autoPaymentThirdInitWriter)
                .transactionManager(platformTransactionManager)
                .taskExecutor(new SimpleAsyncTaskExecutor())
                .throttleLimit(anytxnBatchProperties.getAutoPaymentInitConfiguration().getThrottleLimit())//多线程*/
                .build();
    }

    @Bean
    @StepScope
    public ItemReader<TlPaymentLog> autoPaymentThirdFailInitReader() {
        PrOrganizationTable org = organizationTableService.findByOrgIdForBatch(TransBizConstant.DEFAULT_ORG_ID);
        Date businessDate = org.getNextProcessingDate();
        return new AutoPaymentThirdFailInitReader(businessDate,sqlSessionFactory, anytxnBatchProperties.getAutoPaymentInitConfiguration().getPageSize());
    }

    @Bean
    public Step autoPaymentThirdPartSuccessInitStep(){
        return stepBuilderFactory.get("autoPaymentThirdPartSucessInitStep")
                // 表示一批处理100条记录，数据是逐条发送给processor处理的，处理100条记录后，统一返回给writer进行保存处理
                .<TlPaymentLog, BtAutoPaymentList>chunk(anytxnBatchProperties.getAutoPaymentInitConfiguration().getChunkLimit())
                // 获取上次批扣非足额扣款成功流水
                .reader(autoPaymentThirdPartSuccessInitReader())
                // 处理组装批扣初始化数据
                .processor(autoPaymentThirdInitProcessor)
                // 保存批扣初始化数据
                .writer(autoPaymentThirdInitWriter)
                .transactionManager(platformTransactionManager)
                .taskExecutor(new SimpleAsyncTaskExecutor())
                .throttleLimit(anytxnBatchProperties.getAutoPaymentInitConfiguration().getThrottleLimit())//多线程*/
                .build();
    }

    @Bean
    @StepScope
    public ItemReader<TlPaymentLog> autoPaymentThirdPartSuccessInitReader() {
        PrOrganizationTable org = organizationTableService.findByOrgIdForBatch(TransBizConstant.DEFAULT_ORG_ID);
        Date businessDate = org.getNextProcessingDate();
        return new AutoPaymentThirdPartSuccessInitReader(businessDate,sqlSessionFactory, anytxnBatchProperties.getAutoPaymentInitConfiguration().getPageSize());
    }
}
